package com.example.aslongastoday.ui.tomato_clock;

import android.os.CountDownTimer;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.Locale;

// 定义计时器类
public class CustomCountDownTimer {
    private ProgressBar progressBar; // 进度条
    private TextView textView; // 数字倒计时
    private  Button button; // 开始按钮
    private long millisInFuture; // 计时器总时间
    private long totalTime; //总时间(不是计时器的时间)
    private long countDownInterval; // 计时间隔
    private long timeRemaining; // 剩余时间
    private boolean isPaused; // 是否暂停
    private boolean isCancelled; // 是否取消
    private CountDownTimer countDownTimer; // 倒计时对象

    // 构造函数(重新开始倒计时)
    public CustomCountDownTimer(long millisInFuture, long countDownInterval,
                                ProgressBar progressBar, TextView textView, Button button) {
        this.millisInFuture = millisInFuture;
        this.countDownInterval = countDownInterval;
        this.progressBar = progressBar;
        this.textView = textView;
        this.button = button;
        this.timeRemaining = millisInFuture;
        this.isPaused = false;
        this.isCancelled = false;
    }
    //构造函数(恢复上一次倒计时)
    public CustomCountDownTimer(long timeRemaining, long millisInFuture, long countDownInterval,
                                ProgressBar progressBar, TextView textView, Button button){
        this.totalTime = millisInFuture;
        this.millisInFuture = totalTime;
        this.countDownInterval = countDownInterval;
        this.progressBar = progressBar;
        this.textView = textView;
        this.button = button;
        this.timeRemaining = timeRemaining;
        this.isPaused = false;
        this.isCancelled = false;
    }

    public CustomCountDownTimer() {

    }

    // 开始计时
    public void start() {
        countDownTimer = new CountDownTimer(timeRemaining, countDownInterval) {
            @Override
            public void onTick(long millisUntilFinished) {
                timeRemaining = millisUntilFinished;
                int progress = (int) ((timeRemaining-1000) * 100 / millisInFuture);
                progressBar.setProgress(100-progress);
                textView.setText(String.format(Locale.getDefault(), "%02d:%02d",
                        timeRemaining /1000 / 60, timeRemaining /1000 % 60));
            }

            @Override
            public void onFinish() {
                timeRemaining = 0;
                progressBar.setProgress(0);
                textView.setText("00:00");
                button.setText("开始");
                countDownTimer = null;
            }
        }.start();
        isPaused = false;
        isCancelled = false;
    }

    // 暂停计时
    public void pause() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            isPaused = true;
        }
    }

    // 继续计时
    public void resume() {
        if (isPaused) {
            countDownTimer = new CountDownTimer(timeRemaining, countDownInterval) {
                @Override
                public void onTick(long millisUntilFinished) {
                    timeRemaining = millisUntilFinished;
                    int progress = (int) ((timeRemaining-1000) * 100 / millisInFuture);
                    progressBar.setProgress(100-progress);
                    textView.setText(String.format(Locale.getDefault(), "%02d:%02d",
                            timeRemaining / 60 /1000, timeRemaining /1000 % 60));
                }

                @Override
                public void onFinish() {
                    timeRemaining = 0;
                    progressBar.setProgress(0);
                    textView.setText("45:00");
                    countDownTimer.cancel();
                    button.setText("开始");
                }
            }.start();
            isPaused = false;
        }
    }

    // 重新开始计时
    public void restart() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        timeRemaining = millisInFuture;
        start();
    }

    // 取消计时
    public void cancel() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        isCancelled = true;
    }

    public long getMillisUntilFinished() {
        //剩余时间
        return timeRemaining;
    }

    public long getMillisInFuture() {
        //总时间
        return millisInFuture;
    }
}
